<?php
/**
 * 计划任务
 * User: ws
 * Date: 2018/8/11
 * Time: 18:18
 */

namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Config;
class Planned extends Controller
{

    //自动收货和自动取消
    function orderCancel(){
        $list = Db::name("config")->where("type='o'")->select();
        foreach ($list as $rs){
            $config[$rs['name']] = $rs['value'];
        }

        $order = Db::name('order')->field('order_id,order_status,add_time,shipping_time,type,user_id,pay_price,use_id')->where("order_status=0 or order_status=2")->select();
        foreach ($order as $item){
            if($item['order_status']==0){
                $add_time = floor((time()-$item['add_time'])%86400/3600);
                if((int)$add_time>=(int)$config['day_pay']){
                    $goods_code = Db::table('je_order_goods')->where('order_id',$item['order_id'])->field('code,goods_number,goods_id,spec_item,deposit')->select();
                    $deposit = 0;
                    foreach ($goods_code as $itme){
                        $where['goods_id'] = $itme['goods_id'];
                        $where['price_key'] = $itme['spec_item'];
                        Db::table('je_spec_goods_price')->where($where)->setInc('store', $itme['goods_number']);
                        if(!empty($itme['code'])){
                            $goods_code_id = explode(",",$itme['code']);
                            foreach ($goods_code_id as $val){
                                //Db::table('je_code')->where('code_id',$val)->setField('state', 0);
                                Db::table('je_code')->where('code_id',$val)->update(['state' => 0]);
                            }
                        }
                        $deposit += $itme['deposit'];
                    }

                    if($item['type']==1){
                        //查询免押额度
                        $list = Db::name("config")->where("type='d'")->select();
                        foreach ($list as $rs){
                            $config[$rs['name']] = $rs['value'];
                        }
                        $deposit = $deposit-$item['pay_price'];
                        //查询用户的免押金额和信用
                        $user_jd = db('user')->field('jd_integral,quota')->where("user_id={$item['user_id']}")->find();
                        if((int)$user_jd['jd_integral']>=(int)$config['js_integral'] && (int)$user_jd['quota']>=(int)$config['mortgage']){
                            Db::name('user')->where('user_id', $item['user_id'])->setInc('available_integral', $deposit);
                            Db::name('user')->where('user_id', $item['user_id'])->setDec('frozen_integral',$deposit);
                        }
                    }
                    if($item['use_id']!=0){ Db::table('je_coupons_use')->where('use_id',$item['use_id'])->update(['state' => 0]);}
                    Db::table('je_order')->where('order_id',$item['order_id'])->setField('order_status', 4);
                    $data['order_id'] = $item['order_id'];
                    $data['content'] = '自动取消';
                    $data['create_time'] = time();
                    $data['type'] = 3;
                    $data['u_id'] = 0;
                    db('order_log')->insert($data);
                }
            }else{
                $shipping_time = 1 + ceil((time()-$item['shipping_time'])/60/60/24);//现在的时间减去过去的时间，ceil()进一函数
                if((int)$shipping_time>=(int)$config['day_time']){
                    $tiem = time();
                    if($item['type']==1){
                        Db::table('je_order')->where('order_id',$item['order_id'])->update(['order_status' => 10,'lease_time'=>$tiem,'confirm_time'=>$tiem]);
                    }elseif($item['type']==2){
                        Db::table('je_order')->where('order_id',$item['order_id'])->update(['order_status' => 3,'confirm_time'=>$tiem,'end_time'=>$tiem]);
                    }
                    //Db::table('je_order')->where('order_id',$item['order_id'])->update(['order_status' => 3,'confirm_time'=>$tiem]);
                    $data['order_id'] = $item['order_id'];
                    $data['content'] = '自动收货';
                    $data['create_time'] = $tiem;
                    $data['type'] = 3;
                    $data['u_id'] = 0;
                    db('order_log')->insert($data);

                }
            }
        }


        //会员卡
        $card_order = Db::name('card_order')->field('add_time,use_id,card_order_id')->where("card_status=0")->select();
        foreach ($card_order as $key=>$item){
            $add_time = floor((time()-$item['add_time'])%86400/3600);
            if((int)$add_time>=(int)$config['day_pay']){
                Db::table('je_card_order')->where('card_order_id',$item['card_order_id'])->update(['card_status' => 2]);
                if($item['use_id']!=0){ Db::table('je_coupons_use')->where('use_id',$item['use_id'])->update(['state' => 0]);}
            }
        }

    }

    //会员到期倒计时
    function user_expire(){
        $con = Db::name("config")->where("type='e'")->find();

        //查询会员卡是否在倒计时提示中
        $user = Db::name('user')->where('whether_member=1')->field('user_id,phone,validity_time')->select();

        foreach ($user as $key=>$val){
            //$Days=round(($val['validity_time']-time())/3600/24);
            $Days=floor(($val['validity_time']-time())/86400);
            if($con['value']>=$Days){
                if($Days==0){
                    getMessage(Config::get('chuanglan.account'),Config::get('chuanglan.password'),$val['phone'],Config::get('chuanglan.msgSign').'您好，您的会员卡今天到期，请注意续卡。');
                }elseif($Days<=0){
                    Db::table('je_user')->where('user_id',$val['user_id'])->update(['whether_member' => 2,'validity_time'=>0]);
                }else{
                    getMessage(Config::get('chuanglan.account'),Config::get('chuanglan.password'),$val['phone'],Config::get('chuanglan.msgSign').'您好，您的会员卡还有'.$Days.'天到期，请注意续卡。');
                }
            }

            //加上共享天数
            $w['user_id'] = $val['user_id'];
            $w['order_status'] = 10;
            $order_user = Db::table('je_order')->field('order_id')->where($w)->select();
            foreach ($order_user as $key=>$i){
                $w['order_id'] = $i['order_id'];
                Db::table('je_order')->where($w)->setInc('lease_number');
            }

        }

        //查询会员卡过期，然后把相关的订单加上逾期天数，金额等值
        $user_whether = Db::table('je_user')->where('whether_member',2)->field('user_id,phone')->select();

        foreach ($user_whether as $k=>$item){
            $map['user_id'] = $item['user_id'];
            $map['order_status'] = 10;

            //先加上逾期天数
            Db::table('je_order')->where($map)->setInc('overdue_number');

            //查询租赁中的所有订单
            $user_order = Db::name('order')->field('order_id')->where($map)->select();
            foreach ($user_order as $ke=>$val){
                $where['order_id'] = $val['order_id'];
                //商品名称，用于提示
                $order_goods = Db::table('je_order_goods')->alias('o')->join('je_goods g','o.goods_id = g.goods_id')->where($where)->column('name');
                $order_goods = implode(",",$order_goods);

                //商品一共的租金
                $goodsrent = Db::table('je_order_goods')->where($where)->column('rent');
                $sun = array_sum($goodsrent);
                $number = Db::table('je_order')->where($where)->value('overdue_number');

                //注意， 如果以后价格不对， 请查看是否是浮点数造成的
                $goodsNumber = $sun*$number;

                Db::table('je_order')->where($where)->update(['overdue_price' => $goodsNumber,'overdue_time'=>time()]);
                getMessage(Config::get('chuanglan.account'),Config::get('chuanglan.password'),$item['phone'],Config::get('chuanglan.msgSign').'您好，您所租赁的商品【'.$order_goods.'】，已经逾期'.$number.'天 ，共产生'.$goodsNumber.'元逾期费用，请尽早续卡。');

            }

        }

    }


    //恢复库存时候通知用户， 一分钟查询一次
    function goodsnotice(){
        $shortage = Db::table('je_shortage_goods')->alias('s')->join('je_goods g','s.goods_id = g.goods_id')->join('je_user u','s.user_id = u.user_id')->field('u.phone,g.name,s.shortage_id,s.goods_id')->where(['s.state'=>2])->select();
        foreach ($shortage as $key=>$v){
                $where['goods_id'] = $v['goods_id'];
                $where['store'] = array('gt',0);
                $goods_store = Db::name('spec_goods_price')->where($where)->select();
                if($goods_store){
                    $goods_mag = Config::get('chuanglan.msgSign').'您好，您登记的商品【'.$v['name'].'】现在有货了。';
                    $get_mess = getMessage(Config::get('chuanglan.account'),Config::get('chuanglan.password'),$v['phone'],$goods_mag);
                    if($get_mess==1){
                        Db::table('je_shortage_goods')->where('shortage_id', $v['shortage_id'])->update(['state'=> 0,'content'=>$goods_mag,'notice_time'=>time()]);
                    }else{
                        Db::table('je_shortage_goods')->where('shortage_id', $v['shortage_id'])->update(['state'=> 1,'content'=>$goods_mag,'notice_time'=>time()]);
                    }
                }
        }
    }

    //抽奖次数,一天一次
    function frequencyday(){
        db('user')->where('user_id','gt',0)->update(['treasure_frequency' => 3,'scraping_frequency'=>3,'treasure_forward'=>0,'scraping_forward'=>0]);
    }

    //优惠券
    function coupons(){
        $coupons = db('coupons')->whereTime('settlement_time', '<', time())->field('coupons_id')->select();
        foreach ($coupons as $key=>$itme){
            db('coupons_use')->where('coupons_id',$itme['coupons_id'])->setField('state', 2);
        }
    }

    //解放冻结佣金
    function commission(){
        $list = Db::name("config")->where("type='c'")->find();
        $sj = (int)$list['value'];
        $time = time()-($sj*86400);
        $order = Db::table('je_order')->alias('o')->join('je_commissions c','o.order_id = c.order_id')->field('o.order_id,o.user_id as ouser_id,o.commission as ocmmission,c.upper_level_id,c.commission,c.user_id,c.upper_level_commission')->whereTime('o.end_time', '<', $time)->where('o.type = 2 and o.order_status = 3 and o.distribution_settlement = 0')->select();
        foreach($order as $itme){
            if(!empty($itme['upper_level_id'])){
                db('user')->where('user_id', $itme['upper_level_id'])->setDec('unsettled_money',$itme['upper_level_commission']);
                db('user')->where('user_id', $itme['upper_level_id'])->setInc('distribut_money', $itme['upper_level_commission']);
            }
                db('user')->where('user_id', $itme['user_id'])->setDec('unsettled_money',$itme['commission']);
                db('user')->where('user_id', $itme['user_id'])->setInc('distribut_money', $itme['commission']);
                db('order')->where('order_id',$itme['order_id'])->setField('distribution_settlement', 1);
        }
    }

    //额度计划任务
    function quota(){
        $end = db('user_jdinfo_record')->whereTime('validity_time', '<=', date('Y-m-d'))->where('it_available=1')->select();
        foreach ($end as $key=>$item){
            db('user')->where('user_id', $item['user_id'])->setDec('quota',$item['quota']);
            db('user')->where('user_id', $item['user_id'])->setDec('available_integral',$item['quota']);
            //db('user_jdinfo_record')->where('jdinfo_record_id',$item['jdinfo_record_id'])->update(['it_available' => 2]);
            db('user_jdinfo_record')->where('jdinfo_record_id',$item['jdinfo_record_id'])->setField('it_available', 2);
            if($item['source']==1){
                $data = ['user_id' => $item['user_id'], 'source' => 1,'type'=>1,'create_time'=>time(),'quota'=>$item['quota'],'it_available'=>2];
            }elseif ($item['source']==2){
                $data = ['user_id' => $item['user_id'], 'source' => 2,'type'=>1,'create_time'=>time(),'quota'=>$item['quota'],'it_available'=>2];
            }
            db('user_jdinfo_record')->insert($data);
        }
        $start = db('user_jdinfo_record')->whereTime('add_time', '<=', date('Y-m-d'))->where('it_available=0 and add_time!=0')->select();
        foreach ($start as $k=>$i){
            db('user')->where('user_id', $i['user_id'])->setInc('quota',$i['quota']);
            db('user')->where('user_id', $i['user_id'])->setInc('available_integral',$i['quota']);
            db('user_jdinfo_record')->where('jdinfo_record_id',$i['jdinfo_record_id'])->setField('it_available', 1);
        }

        //会员卡过期
        db('user')->whereTime('validity_time', '<=', time())->where('whether_member=1')->update(['whether_member' => 2,'validity_time'=>0]);

        $spec_goods_price = db('spec_goods_price')->where('deposit','elt',3000)->column('goods_id');
        echo db('spec_goods_price')->getLastSql();die;
        $goodswhere['sale_type'] = 2;
        $goodswhere['shelves'] = 1;
        $goodswhere['goods_id'] = array('in',$spec_goods_price);
        $goodsList = db('goods')->field('goods_id,name as goods_name,small as goods_price,thumbnail_img')->where($goodswhere)->order('sort desc,goods_id')->paginate(6)->toArray();
    }
}
